/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function() {
    var box1;

    $(".product_color").find("li").click(function() {

        var goodsid = $(this).attr("goodsid"),
                attrs = $(this).attr("attrs"),
                userid = $(this).attr("userid"),
                obj = $(this),
                type = $(this).attr("type"),
                c = $(this).attr("c"),
                total_num = $(this).attr("total_num");

        var isNone = $(this).attr("isNone");
        if (isNone == "none") {
            return false;
        }


//        if (a.length != $(".attrs").length && noattrs != 2) {
//            jAlert("请先选择商品属性", "提示");
//            return;
//        }
        changeNums(goodsid, attrs, userid, obj, type, c, total_num)
    })
})


function changeNums(goodsid, attrs, userid, obj, type, c, total_num) {
    $(".product_color").find("li").removeClass("none");
    $(".product_color").find("li").attr("isNone", "");
    var url = url1;
    var current = "attrs_" + type;
    $("#" + current).val(attrs);
    $(".product_color ." + type + "").removeClass("select");
    $(obj).addClass("select");
    var a = new Array();
    $(".attrs").each(function(n, v) {
        if ($(v).val() != '')
        {
            a.push($(v).val());
        }
    });
    var sel_nums = a.length;
    var noattrs = $("#noattributes").val();//是否存在明细
    var attrs1 = a.join(";");
//  alert(attrs+","+goodsid+","+userid);

    //样式变灰
    $.ajax({
        type: "POST",
        url: url2,
        cache: false,
        dataType: "json",
        data: {
            attribute_ids: attrs,
            goods_id: goodsid,
            userid: userid,
            noattrs: noattrs
        },
        success: function(num) {
            /* $(num).each(function(n, v) {
             b.push(v.attribute_ids);
             });*/
            // alert(num.attribute_ids);
            var len1 = num.length;
            for (i = 0; i < len1; i++)
            {
                var attribute_id = num[i]['attribute_ids'];
                var attribute_arr = attribute_id.split(";");
                var len2 = attribute_arr.length;
                if (len2 > 1)
                {
                    for (j = 0; j < len2; j++)
                    {
                        if (attribute_arr[j] != "")
                        {
                            $("." + attribute_arr[j]).addClass("none");
                            $("." + attribute_arr[j]).attr("isNone", "none");
                        }
                    }

                } else {
                    $("." + num[i]['attribute_ids']).addClass("none");
                    $("." + num[i]['attribute_ids']).attr("isNone", "none");
                }

            }

        }
    });


    //样式变灰


    $.ajax({
        type: "POST",
        url: url1,
        cache: false,
        dataType: "json",
        data: {
            attribute_ids: attrs1,
            goods_id: goodsid,
            userid: userid,
            noattrs: noattrs,
            sel_nums: sel_nums,
            total_nums: total_num
        },
        success: function(data) {
            if (data.price != -1) {
                //属性没有全部选择
                $(".price_now").text("￥" + data.price);
                $(".price_no").text("￥" + data.original_price);
            }
           
            $("#num").val(1);
            $("#numers").val(parseInt(data.nums));
            $("#numbers1").text("剩余" + (parseInt(data.nums)) + "件");
            $("#now_num").val(parseInt(data.nums));

            if (data.nums == null || data.nums == 0) {
                $("#numers").val(0);
                $("#numbers1").text("剩余0件");
            }
//            var current_goodsnumbersold = data.goodsnumbersold;
//            var sold = parseInt(current_goodsnumbersold)+parseInt($("#initial_number").val());
//            if(current_goodsnumbersold){
//                 $("#goodsnumbersold").text(sold);
//            }else{
//                 $("#goodsnumbersold").text($("#initial_number").val());
//            }
        }
    });
}

var Products = {
    countnum: function(obj, numbers) {
        if (obj == 1)
            var res = numbers * 1 + 1 - obj * 1;
        else
            var res = numbers * 1 - obj * 1;
        $("#numers").val(res);
        $("#numbers1").text("剩余" + res + "件");
    },
    numbers: function() {

    }
};
//隐藏提示信息并且跳转
function Thref(userid, goodsid)
{
	$("#message").slideUp();
	window.location.href = "/"+userid+"/mobilegoodsdp/productdetails?id=" + goodsid;
}
$(function() {
    var numbers;
    var url = url1;
    $("#del").click(function() {
        var inputnum = $("#num").val(); //输入购物商品数量
        var numbers = $("#numers").val(); //库存
        if (inputnum > 1) {
            if (inputnum == 2)
                var res_num = numbers * 1 + 2;
            else
                var res_num = numbers * 1 + 1;
            var res_input = inputnum * 1 - 1;
            $("#num").val(res_input);
            $("#numers").val(res_num);
            $("#numbers1").text("剩余" + res_num + "件");
        }

    });
    $("#add").click(function() {
        var inputnum = $("#num").val(); //输入购物商品数量
        var numbers = $("#numers").val(); //库存
        var noattrs = $("#noattributes").val(); //是否存在明细
        var a = new Array();
        $(".attrs").each(function(n, v) {
            if ($(v).val() != '')
            {
                a.push($(v).val());
            }
        });
        var total_nums = $("#total_nums").val();
        var sel_nums = a.length;
        var userid = $("#userid").val();
        var goodsid = $("#productid").val();
        var attrs = a.join(";");
        if (a.length != $(".attrs").length && noattrs != 2) {
            jAlert("请先选择商品属性", "提示");
            return;
        }
        /*if (($("#from_type").val() == 4) && (parseInt(inputnum) + 1 > $('#limit_by').val())) {
         jAlert("每件物品限购" + $('#limit_by').val() + "件!", "提示");
         return;
         }*/
        $.ajax({
            type: "POST",
            url: url1,
            cache: false,
            dataType: "json",
            async: false,
            data: {
                attribute_ids: attrs,
                goods_id: goodsid,
                userid: userid,
                noattrs: noattrs,
                sel_nums: sel_nums,
                total_nums: total_nums
            },
            success: function(data) {
                var num = data.nums;
                if (inputnum * 1 < num * 1) {
                    var res_input = inputnum * 1 + 1;
                    if (res_input == 2)
                        var res_num = numbers * 1 - 2;
                    else
                        var res_num = numbers * 1 - 1;
                    $("#numers").val(res_num);
                    $("#numbers1").text("剩余" + res_num + "件");
                    $("#num").val(res_input);
                } else {
                    $("#numbers1").text("剩余" + 0 + "件");
                }
            }
        });
    });
    $("#num").blur(function() {
        var num = $(this).val();
        var reg = new RegExp("^[0-9]*$");
        if (!reg.test(num) || num == 0) {
            $("#num").val(1);
        }

    });
    $('#num').on('input', function() {
        var inputnum = $("#num").val(); //输入购物商品数量
        var noattrs = $("#noattributes").val(); //是否存在明细
        var reg = new RegExp("^[0-9]*$");
        var a = new Array();
        $(".attrs").each(function(n, v) {
            a.push($(v).val());
        });
        var userid = $("#userid").val();
        var goodsid = $("#productid").val();
        var attrs = a.join(";");
        if (a == ",") {
            jAlert("请先选择商品属性", "提示");
            $("#num").val(1);
            return;
        }
        /* if (($("#from_type").val() == 4) && (parseInt(inputnum) + 1 > $('#limit_by').val())) {
         jAlert("每件物品限购" + $('#limit_by').val() + "件!", "提示");
         $("#num").val($('#limit_by').val());
         $("#numbers1").text("剩余" + ($("#now_num").val() - $('#limit_by').val()) + "件");
         return;
         }*/
        var total_nums = $("#total_nums").val();
        var sel_nums = a.length;
        $.ajax({
            type: "POST",
            url: url1,
            cache: false,
            dataType: "json",
            async: false,
            data: {
                attribute_ids: attrs,
                goods_id: goodsid,
                userid: userid,
                noattrs: noattrs,
                sel_nums: sel_nums,
                total_nums: total_nums
            },
            success: function(data) {
                var countnum = data.nums;
                if (inputnum != "") {
                    if (!reg.test(inputnum) || inputnum == 0) {
                        $("#num").val(1);
                    }
                }

                if (countnum * 1 < inputnum * 1) {
                    jAlert("库存不够", "提示");
                    $("#num").val(1);
                    var res_num = countnum * 1 - 1;
                    $("#numbers1").text("剩余" + res_num + "件");
                    return;
                }
                if (countnum == null) {
                    $("#numbers1").text("剩余0件");
                    $("#num").val(0);
                } else {
                    if (num * 1 > countnum * 1) {
                        $("#num").val(1);
                        $("#numbers1").text("剩余0件");
                    }
                    numbers = countnum;
                    Products.countnum($("#num").val(), numbers);
                }
            }
        });
    });
//for ie
    if (document.all) {
        $('input[type="text"]').each(function() {
            var that = this;
            if (this.attachEvent) {
                this.attachEvent('onpropertychange', function(e) {
                    if (e.propertyName != 'value')
                        return;
                    $(that).trigger('input');
                });
            }
        })
    }
    
    
//点击购物车
    $("#goods_cat").click(function() {
        var a = new Array();
        $(".attrs").each(function(n, v) {
            if ($(v).val() != '')
            {
                a.push($(v).val());
            }
        });
        var total_nums = $("#total_nums").val();
        var sel_nums = a.length;
        var attrs = a.join(";");
        var goodsid = $("#productid").val();
        var attrs = a.join(";");
        var userid = $("#userid").val();
        var noattrs = $("#noattributes").val(); //是否存在明细
        if (noattrs == 1) {
            if (a.length != $(".attrs").length && noattrs != 2) {
                jAlert("请先选择商品属性", "提示");
                return false;
            }
        }
        $.ajax({
            type: "POST",
            url: url1,
            cache: false,
            dataType: "json",
            async: false,
            data: {
                attribute_ids: attrs,
                goods_id: goodsid,
                userid: userid,
                noattrs: noattrs,
                sel_nums: sel_nums,
                total_nums: total_nums
            },
            success: function(data) {
                var num = data.nums;
                if (num <= 0) {
                    jAlert("你选择的商品已无货", "提示");
                    return false;
                } else {
                	
                    var number = $("#num").val();
                    $.ajax({
                        type: "POST",
                        url: "/mobilegoodsdp/goodscat",
                        cache: false,
                        dataType: "json",
                        data: {
                            attribute_ids: attrs,
                            goods_id: goodsid,
                            numer: number
                        },
                        success: function(data) {
                            if (data.code == 1) 
                            {	
                                $("#message").text("已成功加入购物车");
                             
                            	setTimeout("Thref("+userid+", "+goodsid+")", 1000);
                            } 
                            else if (data.code == 0) 
                            {
                                jAlert(data.msg, "提示");
                            }
                          
                        }
                    });
                }
            }
        });
    });
    
    //立即购买
    $("#buy").click(function() {
        var goodsid = $("#productid").val();
        var userid = $("#userid").val();
        var noattrs = $("#noattributes").val(); //是否存在明细
        var limitNum = parseInt($('#limit_num').val());//限购数量
        var goodsNum = $('#num').val();
        var a = new Array();
        $(".attrs").each(function(n, v) 
        {
            if ($(v).val() != '')
            {
                a.push($(v).val());
            }
        });
        var total_nums = $("#total_nums").val();
        var sel_nums = a.length;
        var attrs = a.join(";");
        if (a.length != $(".attrs").length && noattrs != 2) {
            jAlert("请先选择商品属性", "提示");
            return;
        }


        if(limitNum > 0 && goodsNum > limitNum)
        {
        	jAlert("此商品每人限购"+limitNum+"个", "提示");
            return;
        }
        
        $.ajax({
            type: "POST",
            url: url1,
            cache: false,
            dataType: "json",
            data: {
                attribute_ids: attrs,
                goods_id: goodsid,
                userid: userid,
                noattrs: noattrs,
                sel_nums: sel_nums,
                total_nums: total_nums
            },
            success: function(data) {
            	console.log(data);
            	if(data.limit_number)
            	{
            		var userNum = parseInt(data.limit_number) + parseInt(goodsNum);
            		 //if(data.openid == 2026) {alert('userNum=>'+userNum);}//张乐调试专用，正式部署要删除
            		
            		if(limitNum > 0 && userNum > limitNum)
            		{
            			jAlert("此商品每人限购"+limitNum+"个", "提示");
                        return false;
            		}
            		
            	}
            	
            	
                var num = data.nums;
                if (num <= 0) {
                    jAlert("你选择的商品已无货", "提示");
                    return false;
                } else {
                    var goodsid = $("#productid").val();
                    var num = $("#num").val();
                    var noattributes = $("#noattributes").val();
                    window.location.href = "/"+$("#userid").val()+"/mobilegoodsdp/buy?fxuserid="+data.fxuserid+"&goods_id=" + goodsid + "&salenums=" + num + "&ids=" + attrs + "&noattributes=" + noattributes;
                }
            }
            });
    });

    $("#pay_type").change(function() {
        var v = $(this).val();
        $("#createorder").removeClass("silverOrder");
        $("#createorder").addClass("order_btn");
       
        //找人代付
        if(v == '12'){
             $("#daifu_content").attr("style", "display:block");
             $("#time_content").attr("style", "display:none;");
        }else{
             $("#time_content").attr("style", "display:block;");
             $("#daifu_content").attr("style", "display:none");
        }
        if (v == '6') {
            $("#time_content").attr("style", "display:none;");
        } else {
             if(v == '12'){
                $("#time_content").attr("style", "display:none;");
            }else{
                $("#time_content").attr("style", "display:block;");
            }
        }
    });
});
function close1() {
    $.alerts._hide()
}

//改变金额
function changeJine(type, jine, postage) {
    var a = new Array();
    a = type.split("_");
    var flg = a[1];
    var price = a[2];
    var is_come = $("#is_arayacak").val();
    if(is_come == 1){
           var is_checked = 'checked';
    }
    if(is_checked == 'checked'){
        if (flg == 3) {
            //现金券
            var result = jine * 1 - price * 1;
            $("#fancoupons").text(price);
            var spare_amount = $("#fancoupons").text();
            if (result * 1 <= 0 * 1) {
                    $("#cost").text("0.00");
                    $("#order_amount").val("0.00");
                    $("#spare_amount").val(spare_amount);
                    $("#pay_type").attr("style", "display:none");
                    $("#pay_type_1").attr("style", "display:block");
            } else {
                $("#cost").text((result * 1 + postage * 1).toFixed(2));
                $("#order_amount").val((result * 1 + postage * 1).toFixed(2));
                $("#spare_amount").val(spare_amount);
                $("#pay_type").attr("style", "display:block");
                $("#pay_type_1").attr("style", "display:none");
            }
        }
        if (flg == 2) {
            //打折券
            var preprice = jine * 1 * price * 1;
            var preferential_price = jine * 1 - preprice * 1;
            $("#fancoupons").text((preferential_price).toFixed(2));
            var spare_amount = $("#fancoupons").text();
            $("#cost").text((preprice * 1).toFixed(2));
            $("#spare_amount").val(spare_amount);
            $("#order_amount").val((preprice * 1).toFixed(2));
            $("#pay_type").attr("style", "display:block");
            $("#pay_type_1").attr("style", "display:none");
        }
        if (type == 0) {
            $("#fancoupons").text("0.00");
            $("#cost").text((jine * 1).toFixed(2));
            var spare_amount = $("#fancoupons").text();
            $("#spare_amount").val(spare_amount);
            $("#order_amount").val((jine * 1).toFixed(2));
            $("#pay_type").attr("style", "display:block");
            $("#pay_type_1").attr("style", "display:none");
        }
    }else{
        if (flg == 3) {
            //现金券
            var result = jine * 1 - price * 1;
            $("#fancoupons").text(price);
            var spare_amount = $("#fancoupons").text();
            if (result * 1 <= 0 * 1) {
                if (postage * 1 > 0) {
                    $("#cost").text(postage.toFixed(2));
                    $("#order_amount").val(postage.toFixed(2));
                    $("#spare_amount").val(spare_amount);
                } else {
                    $("#cost").text("0.00");
                    $("#order_amount").val("0.00");
                    $("#spare_amount").val(spare_amount);
                    $("#pay_type").attr("style", "display:none");
                    $("#pay_type_1").attr("style", "display:block");
                }
            } else {
                $("#cost").text((result * 1 + postage * 1).toFixed(2));
                $("#order_amount").val((result * 1 + postage * 1).toFixed(2));
                $("#spare_amount").val(spare_amount);
                $("#pay_type").attr("style", "display:block");
                $("#pay_type_1").attr("style", "display:none");
            }
        }
        if (flg == 2) {
    //打折券
            var preprice = jine * 1 * price * 1;
            var preferential_price = jine * 1 - preprice * 1;
            $("#fancoupons").text((preferential_price).toFixed(2));
            var spare_amount = $("#fancoupons").text();
            $("#cost").text((preprice * 1 + postage * 1).toFixed(2));
            $("#spare_amount").val(spare_amount);
            $("#order_amount").val((preprice * 1 + postage * 1).toFixed(2));
            $("#pay_type").attr("style", "display:block");
            $("#pay_type_1").attr("style", "display:none");
        }
        if (type == 0) {
            $("#fancoupons").text("0.00");
            $("#cost").text((jine * 1 + postage * 1).toFixed(2));
            var spare_amount = $("#fancoupons").text();
            $("#spare_amount").val(spare_amount);
            $("#order_amount").val((jine * 1 + postage * 1).toFixed(2));
            $("#pay_type").attr("style", "display:block");
            $("#pay_type_1").attr("style", "display:none");
        }
     }
}
$(document).ready(function() {
    var shipping_address = $("#linkman_id").val();
    var isSelect = true, hasSel = true;
    $("#fancoupons_type").click(function() {
        var list = $(this).find("option");
        for (var i = 0; i < list.length; i++) {
            if (list.eq(i).text() == "取消兑换券") {
                hasSel = false;
            }
            ;

        }
        ;
        // if (shipping_address == "") {
            // jAlert("请填写收货地址", "提示");
            // return false;
        // }
        var pay_type = $("#fancoupons_type").val();
        var type_array = pay_type.split("_");
        var p_type = type_array[1];

        if (p_type == 0 || p_type == undefined) {
            if (hasSel) {
                isSelect = false;
            }
        } else {
            if (!isSelect) {
                jQuery("#fancoupons_type").prepend("<option value='0'>取消兑换券</option>");
            }
            isSelect = true;
        }
    });


//    $("#pay_type").click(function() {
//        if (shipping_address == "") {
//            jAlert("请填写收货地址", "提示");
//            return false;
//        }
//
//    });

    //兑换券密码关闭弹框
    $("#close_payment").click(function() {
        box1.close();
    });

    //付款
    $('#payment_now').live('click', function() {
        var fan_id;
        var attrids = $("#attrids").val();
        var goodsid = $("#goods_id").val();
        var userid = $("#userid").val();
        var noattributes = $("#noattributes").val();
        var buynums = $("#goods_nums").val();
        var fancoupons_type = $("#fancoupons_type").val();
        if (fancoupons_type != undefined) {
            if (fancoupons_type == 0) {
                fan_id = 0;
            } else {
                var a = new Array();
                a = fancoupons_type.split("_");
                fan_id = a[0];
            }
        }
        var total_nums = 1;
        var sel_nums = 1;
        var isSuccess = true;
        $.ajax({
            type: "POST",
            url: url1,
            cache: false,
            dataType: "json",
            data: {
                attribute_ids: attrids,
                goods_id: goodsid,
                userid: userid,
                sel_nums: sel_nums,
                total_nums: total_nums,
                fan_id: fan_id,
                noattrs: noattributes
            },
            success: function(data) {
                var num = data.nums;
                var type = data.type;
                if (num * 1 < buynums * 1) {
                    isSuccess = false;
                    jAlert("库存不够", "提示");
                } else if (type == 0) {
                    if (fancoupons_type != 0) {
                        isSuccess = false;
                        jAlert("兑换券已使用", "提示");
                    } else {
                    }
                } else {
                    
                    //兑换券密码是否正确
                    var pw = $('#pwd').val();
                    if (pw == '') {
                        isSuccess = false;
                        jAlert("请输入支付密码", "提示");
                    } else {
                        isSuccess = false;
                        $.ajax({
                            url: "/mobilegoodsdp/checkpw",
                            type: "post",
                            dataType: 'json',
                            data: {'pw': pw},
                            success: function(obj) {
                                if (obj == 1) {
                                    var pay_type = $("#pay_type").val();
                                    var order_amount = $("#order_amount").val();
                                    if(pay_type == 9 ){
                                                        $("#payment_now").addClass("greybtn"); //付款按钮毁掉
                                                        $("#payment_now").removeClass("orangebtn"); //付款按钮毁掉
                                                        $("#payment_now").attr("disabled","disabled");
                                                        $("#payment_now").attr("id","");
                                                        
                                                        $("#createorder").removeClass("order_btn");
                                                        $("#createorder").addClass("silverOrder");
 														$("#createorder").attr("disabled","disabled");
                                                        $.ajax({
                                                            type : "post",
                                                            data : {
                                                                    'total_amount':order_amount
                                                            },
                                                            url : "/mobilegoodsdp/checkfancardmoney",
                                                            dataType:"json",
                                                            success :function(data) {
                                                                if(data.state==10)
                                                                        {
                                                                                box1.close();
                                                                                $("#one_order").submit();
                                                                                setTimeout(function() {
                                                                                                $("#fancoupons_type").val(0);
                                                                                 }, 500);
                                                                        }else if(data.state==5){
                                                                                 jAlert(data.cont, "提示");
//                                                                                isSuccess = false;
                                                                                  box1.close();
                                                                        }else if(data.state==6){
                                                                                  jAlert(data.cont, "提示");
//                                                                                isSuccess = false;
                                                                                  box1.close();
                                                                        }               
                                                            } 
                                                            });
                                            }else{
                                                 $("#payment_now").addClass("silverOrder"); //付款按钮毁掉
                                                 $("#createorder").removeClass("order_btn");
                                                 $("#createorder").addClass("silverOrder");
                                                 $("#createorder").val("订单提交中");
                                                 isSuccess = true;
                                            }
                                } else {
                                    jAlert("支付密码不正确", "提示");
                                    isSuccess = false;
                                }
                                if (isSuccess && $("#pay_type").val()!= 9 ) {
                                    box1.close();
                                    $("#one_order").submit();
                                    setTimeout(function() {
                                        $("#fancoupons_type").val(0);
                                    }, 500);
                                }
                            }
                        });
                    }
                }
            }
        });
    });

    $("#forgetpassword").click(function(){
        var userid = $("#userid").val();
        var attrids = $("#attrids").val();
        var goodsid = $("#goods_id").val();
        var buynums = $("#goods_nums").val();
        var fancoupons_type = $("#fancoupons_type").val();
        if(fancoupons_type==undefined){
        	fancoupons_type="";
        }
        var pay_type;
        if ($("#pay_type").attr("style") == 'display:block') {
            pay_type = $("#pay_type").val();
        }else{
            pay_type = $("#pay_type_1").val();
        }
        var shipping_address = $("#linkman_id").val();
        $.ajax({
                    type: "POST",
                    url: "/mobilegoodsdp/forgetpassword",
                    cache: false,
                    dataType: "json",
                    data: {
                    	fancoupons_type: fancoupons_type,
                        ids: attrids,
                        goods_id: goodsid,
                        salenums: buynums,
                        shippingid: shipping_address,
                        pay_type:pay_type,
                        type:1
                    },
                    success: function(data) {
                        window.location="/"+data+"/memberfront/setpassword?type=1";
                    }
                });
    });


    //兑换券密码
//    $('.createorder').live('click', function() {
    $(".createorder").click(function() {
        var $id = $(this);
        var fan_id;
        var shipping_address = $("#address").val();

        var come_type = $("#is_arayacak").val();

        var come_type_flg = come_type;
        var fxopenid = $id.attr("data_fxopenid");
        var data_is_virtua = $id.attr("data_is_virtua");
        if(fxopenid>0&&come_type==undefined&&data_is_virtua==1){
            come_type=1;
        }else if(fxopenid>0&&come_type==undefined&&data_is_virtua!=1){
            come_type=0;
        }else if(fxopenid<=0){
            come_type=1;
        };

        if( come_type != undefined && come_type == 0){
            if (shipping_address == "") {
                jAlert("请填写收货地址", "提示");
                return false;
            }
        }

        if((data_is_virtua == 1 && $("#mr_username").val()=="") ||　$("#username").val()==""){
             jAlert("请填写您的姓名", "提示");
             return false;
        }
        if((data_is_virtua == 1 && $("#mr_mobilephone").val()=="") || $("#mobilephone").val()==""){
            jAlert("请填写您的联系电话", "提示");
            return false;
        }else{
            if($("#mobilephone").val()!=undefined){
                 var mobile = /(^1[3|4|5|7|8][0-9]\d{8}$)|(^0\d{2,3}-?\d{7,8}$)|((^400(-\d{3,4}){2})$)|((^400(\d{7}))$)|((^800(-\d{3,4}){2})$)|((^800(\d{7}))$)/;
                var prompt = "请输入合法的电话或手机!";
                if (!mobile.test($("#mobilephone").val())) {
                    jAlert(prompt, "提示");
                    return false;
                }
            }
        }

        //if(come_type_flg==0  || fxopenid <= 0){
        //    if($("#address").val()==""){
        //        jAlert("请填写您的收货地址", "提示");
        //        return false;
        //    }
        //}

        if ($("#pay_type").attr("style") == 'display:block') {
            var pay_type = $("#pay_type").val();
            if (pay_type == 0) {
                jAlert("请先选择支付方式", "提示");
                return false;
            }
        }
        var fancoupons_type = $("#fancoupons_type").val();
        var attrids = $("#attrids").val();
        var goodsid = $("#goods_id").val();
        var userid = $("#userid").val();
        var noattributes = $("#noattributes").val();
        var buynums = $("#goods_nums").val();

		//千人大会S
		//if(goodsid==1371 && userid==177 && fxopenid<=0 )
		//if(userid==69 && fxopenid<=0 && (goodsid==623  || goodsid==624 || goodsid==625) )
		if(userid==102 && fxopenid<=0 && (goodsid==40389  || goodsid==40391 || goodsid==40786) )
		{
			jAlert("请先关注后,通过图文再购买！", "提示");
			//alertshow('alert2');
			return false;
		}
		//if(goodsid==1371 && userid==177 && buynums>1 )
		//if(userid==69 && buynums>1 && (goodsid==623  || goodsid==624 || goodsid==625) )
		if(userid==102 && buynums>1 && (goodsid==40389  || goodsid==40391 || goodsid==40786) )
		{
			jAlert("门票仅限购一张！", "提示");
			return false;
		}

        if(fxopenid <= 0 && $("#is_arayacak").val() == 0){
            if($("#province").val() ==0){
                alert("省份不能为空");
                return false;
            }
            if($("#city").val()==0){
                alert("区/县不能为空");
                return false;
            }
            if($("#district").val()==0){
                alert("城市不能为空");
                return false;
            }
        }

		//if(goodsid==1371 && userid==177)
		//if(userid==69 && (goodsid==623  || goodsid==624 || goodsid==625) )
		if(userid==102 && (goodsid==40389  || goodsid==40391 || goodsid==40786) )
		{
			var isbuy_qrdhmp=0;
//			var qrdh_phone=$("#mobilephone").val();
                        var qrdh_phone=document.getElementById("mobilephone").value;
			$.ajax({
					  type: "POST",
					  url: "/mobilegoodsdp/checkqrdhmp",
					  cache: false,
					  async : false,
					  dataType: "json",
					  data: {
						  qrdh_phone: qrdh_phone,
					  },
					  success: function(data) {
						  if(!data)
						  {
							  isbuy_qrdhmp=1;
						  }
					  }
			 });
		}
		if(isbuy_qrdhmp==1)
		{
			 jAlert("您已购买门票！", "提示");
			return false;
		}
		//if(userid==69 && (goodsid==623  || goodsid==624 || goodsid==625) )
		if(userid==102 && (goodsid==40389  || goodsid==40391 || goodsid==40786) )
		{
			var isbuy_qrdhmp_stock=0;
//			var qrdh_present_price=$("#present_price").val();
                        var qrdh_present_price=document.getElementById("present_price").value;

			$.ajax({
					  type: "POST",
					  url: "/mobilegoodsdp/checkqrdhmpstock",
					  cache: false,
					  async : false,
					  dataType: "json",
					  data: {
						  qrdh_present_price: qrdh_present_price,
					  },
					  success: function(data) {
						  if(!data)
						  {
							  isbuy_qrdhmp_stock=1;
						  }
					  }
			 });
		}
		if(isbuy_qrdhmp_stock==1)
		{
			 jAlert("门票已售完！", "提示");
			return false;
		}
		//千人大会E

        var total_nums = 1;
        var sel_nums = 1;
        fan_id = 0;
        var isSuccess=true;
        //检查商品是否下架
         $.ajax({
            url: "/mobilegoodsdp/checkgoods",
            type: "post",
            dataType: 'json',
            data: {'goodsid': goodsid},
            success: function(obj) {
                if (obj == '1') {
                    if (fancoupons_type != 0 && fancoupons_type != undefined) {
                    box1 = $.dialog({
                        title: '请输入支付密码',
                        content: document.getElementById('payment_box'),
                        lock: true,
                        padding: 0,
                        fixed: false
                    });
            }else{
                    if(pay_type == 9){
                        box1 = $.dialog({
                            title: '请输入支付密码',
                            content: document.getElementById('payment_box'),
                            lock: true,
                            padding: 0,
                            fixed: false
                        });
                    }else{
                        $.ajax({
                            type: "POST",
                            url: url1,
                            cache: false,
                            dataType: "json",
                            data: {
                                attribute_ids: attrids,
                                goods_id: goodsid,
                                userid: userid,
                                sel_nums: sel_nums,
                                total_nums: total_nums,
                                fan_id: fan_id,
                                noattrs: noattributes
                            },
                            success: function(data) {
                                var num = data.nums;
                                var type = data.type;
                                if (num * 1 < buynums * 1) {
                                    isSuccess = false;
                                    jAlert("库存不够", "提示");
                                }
                                if (isSuccess) {

									//千人大会S
									//if(goodsid==1371 && userid==177 && fxopenid>0 )
									//if(userid==69 && fxopenid>0 && (goodsid==623  || goodsid==624 || goodsid==625) )
									if(userid==102 && fxopenid>0 && (goodsid==40389  || goodsid==40391 || goodsid==40786) )
									{
											$("#qrdh_phone").html(qrdh_phone);
											alertshow('alert');
											return false;
									}
									//千人大会E

                                    $("#createorder").removeClass("order_btn");
                                    $("#createorder").addClass("silverOrder");
                                    $("#createorder").html("订单提交中");
                                    $id.removeAttr("id");
                                    $("#one_order").submit();
                                    setTimeout(function() {
                                        $("#fancoupons_type").val(0);
                                    }, 500);
                                }
                            }
                        });
                       }
            }
                } else {
                    jAlert("商品已下架", "提示");
                }
            }
        });
        //检查商品是否下架
    });
});

//找人代付弹出层
function alertshow(divDisplay)
{
    if(document.getElementById(divDisplay).style.display == "none")
    {
      document.getElementById(divDisplay).style.display = "block";
    }
    else
    {
     document.getElementById(divDisplay).style.display = "none";
    }
}  
//找人代付弹出层

function checkpassword() {
    var pw = $('#pwd').val();
    if (pw == '') {
        isSuccess = false;
        alert('请输入支付密码');
    } else {
        $.ajax({
            url: "{{$baseUrl}}/phoneclubcard/checkpw",
            type: "post",
            dataType: 'json',
            data: {'pw': pw},
            success: function(obj) {
                if (obj == 1) {
                    isSuccess = true;
                } else {
                    alert('支付密码不正确');
                    isSuccess = false;
                }
            }
        });
    }
    return isSuccess;
}







